package com.chencong.transform;

import com.chencong.env.FlinkTableEnv;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.List;

/**
 * @Author chencong
 * @Description
 * @Date 8:07 下午 2021/8/17
 * @Param
 **/
public class Flatmap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment streamTableEnvironment = FlinkTableEnv.getStreamTableEnvironment();
        streamTableEnvironment.setParallelism(1);

        // TODO flatmap 压平：一进多出、一进零出
        // 1. 可以实现类似过滤的效果， 不满足条件，就不用采集器往下游发送
        streamTableEnvironment
                .fromElements(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8))
                .flatMap(new FlatMapFunction<List<Integer>, String>() {
                    @Override
                    public void flatMap(List<Integer> value, Collector<String> out) throws Exception {
                        for (Integer num : value) {
                            if (num % 2 == 0) {
                                out.collect(num + "");
                            }
                        }
                    }
                })
                .print();
        streamTableEnvironment.execute();
    }

}
